package com.zidongxiangxi.rabbit.starter.configuration.consumer;

import com.zidongxiangxi.rabbit.consumer.interceptor.IdempotentOperationsInterceptor;
import com.zidongxiangxi.rabbit.consumer.interceptor.SequenceOperationsInterceptor;
import com.zidongxiangxi.rabbit.consumer.support.RabbitListenerContainerBeanPostProcessor;
import com.zidongxiangxi.rabbit.consumer.support.SimpleRabbitListenerContainerFactoryBeanPostProcessor;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * description
 *
 * @author chenxudong
 * @date 2020/11/08
 */
@Configuration
@ConditionalOnClass(SimpleMessageListenerContainer.class)
@Import({ReliableRabbitConsumerIdempotentConfiguration.class})
public class ReliableRabbitConsumerAutoConfiguration {
	/**
	 * 定义rabbit的simple监听容器工厂bean的后置加工
	 * 该加工厂将“幂等消费拦截器”和“顺序消费拦截器”的bean设置到工厂中
	 *
	 * @return rabbit的simple监听容器工厂bean的后置加工
	 */
	@Bean
	public SimpleRabbitListenerContainerFactoryBeanPostProcessor simpleRabbitListenerContainerFactoryBeanPostProcessor(
			ObjectProvider<SequenceOperationsInterceptor> sequenceInterceptorProvider,
			ObjectProvider<IdempotentOperationsInterceptor> idempotentInterceptorProvider
	) {
		return new SimpleRabbitListenerContainerFactoryBeanPostProcessor(sequenceInterceptorProvider, idempotentInterceptorProvider);
	}

	/**
	 * 定义rabbit的监听容器bean的后置加工
	 * 该加工厂将“幂等消费拦截器”和“顺序消费拦截器”的bean设置到监听容器中
	 *
	 * @return rabbit的监听容器bean的后置加工
	 */
	@Bean
	public RabbitListenerContainerBeanPostProcessor rabbitListenerContainerBeanPostProcessor(
			ObjectProvider<SequenceOperationsInterceptor> sequenceInterceptorProvider,
			ObjectProvider<IdempotentOperationsInterceptor> idempotentInterceptorProvider
	) {
		return new RabbitListenerContainerBeanPostProcessor(sequenceInterceptorProvider, idempotentInterceptorProvider);
	}
}
